package com.xiaoc.bulldozer.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.xc.framework.cache.manager.CacheManager;
import com.xc.framework.cache.utils.CacheUtils;
import com.xiaoc.bulldozer.common.CacheKeys;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.dao.ConvertWeixinUserMapper;
import com.xiaoc.bulldozer.common.dto.ConvertWeixinUserDto;
import com.xiaoc.bulldozer.common.dto.ConvertWeixinUserQueryDto;
import com.xiaoc.bulldozer.common.model.ConvertWeixinUser;
import com.xiaoc.bulldozer.common.service.ConvertWeixinUserService;

/**
 * ConvertWeixinUser Service 实现类
 * 
 * @author Danfo Yam
 * 
 * @date 2018-09-06 13:29:52
 */
@Service("convertWeixinUserService")
public class ConvertWeixinUserServiceImpl implements ConvertWeixinUserService {

    @Resource
    private ConvertWeixinUserMapper convertWeixinUserMapper;
    
    @Resource(name = "remoteCacheManager")
    private CacheManager remoteCacheManager;

    @Override
    public Integer getConvertWeixinUserCountByCondition(ConvertWeixinUserQueryDto convertWeixinUserQueryDto) {
        return convertWeixinUserMapper.getConvertWeixinUserCountByCondition(convertWeixinUserQueryDto);
    }

    @Override
    public List<ConvertWeixinUser> getConvertWeixinUserListByCondition(ConvertWeixinUserQueryDto convertWeixinUserQueryDto) {
        convertWeixinUserQueryDto.setPage(convertWeixinUserQueryDto.getPage());
        return convertWeixinUserMapper.getConvertWeixinUserListByCondition(convertWeixinUserQueryDto);
    }

    @Override
    public ConvertWeixinUser getConvertWeixinUserById(Integer id) {
        return convertWeixinUserMapper.getConvertWeixinUserById(id);
    }

    @Override
    public List<ConvertWeixinUser> getAllConvertWeixinUserList() {
        return convertWeixinUserMapper.getAllConvertWeixinUserList();
    }
    
    @Override
    public void save(ConvertWeixinUser convertWeixinUser) {
        convertWeixinUserMapper.save(convertWeixinUser);
    }

    @Override
    public void update(ConvertWeixinUser convertWeixinUser) {
        convertWeixinUserMapper.update(convertWeixinUser);
    }

    @Override
    public void deleteById(Integer id) {
        convertWeixinUserMapper.deleteById(id);
    }
    
    @Override
    public ConvertWeixinUserDto getConvertWeixinUserByOpenId(String openId) {
        return convertWeixinUserMapper.getConvertWeixinUserByOpenId(openId);
    }
    
    @Override
    public ConvertWeixinUserDto getConvertWeixinUserDtoBySessionId(String sessionId) {
        return StringUtils.isBlank(sessionId) ? null : (ConvertWeixinUserDto) remoteCacheManager.get(CacheUtils.generateCacheKey(CacheKeys.SESSION_WEIXIN_USER_KEY, sessionId));
    }

    @Override
    public void putConvertWeixinUserDto(String sessionId, ConvertWeixinUserDto convertWeixinUserDto) {
        convertWeixinUserDto.setSessionTime(System.currentTimeMillis());
        remoteCacheManager.put(CacheUtils.generateCacheKey(CacheKeys.SESSION_WEIXIN_USER_KEY, sessionId), convertWeixinUserDto, Constant.SESSION_TIMEOUT);
    }
    
    @Override
    public List<ConvertWeixinUser> getConvertWeixinUserListByPhoneNumber(String phoneNumber) {
        return convertWeixinUserMapper.getConvertWeixinUserListByPhoneNumber(phoneNumber);
    }

}
